package logic

import (
	"common/model"
	"context"
	"errors"
	user "user_srv/proto"
)

func (u *UserServer) UserCouponList(ctx context.Context, req *user.UserCouponListRequest) (*user.UserCouponListResponse, error) {
	if req.UserCouponStatus != "" && req.UserCouponStatus != "已过期" && req.UserCouponStatus != "未过期" && req.UserCouponStatus != "已使用" {
		return nil, errors.New("请输入合理的优惠券使用状态")
	}
	var UserCoupons model.UserCoupon
	res, err := UserCoupons.GetCouponByUserIdAndStatus(int(req.Userid), req.UserCouponStatus)
	if err != nil {
		return nil, err
	}
	if len(res) == 0 {
		return nil, errors.New("暂时没有这种状态的优惠券")
	}
	var allUserCoupon []*user.UserCouponListInfo
	for _, v := range res {
		allUserCoupon = append(allUserCoupon, &user.UserCouponListInfo{
			CouponId:          int64(v.CouponId),
			CouponPrice:       float32(v.CouponPrice),
			HowMoneyAvailable: float32(v.HowMoneyAvailable),
			Status:            v.Status,
		})
	}
	return &user.UserCouponListResponse{AllUserCoupon: allUserCoupon}, nil
}
